package com.org.zp.ctrl.myzhaopin;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.app.RecApp;
import com.org.zp.entity.myzp.ResumeInfo;

public class BrowseResumeActivity extends BaseActivity {
	private RecApp mApp;

	private TextView headerTitle;
	private Button backbutton, top_right_btn;

	private TextView myzp_browse_resume_name, myzp_browse_resume_user_name,
			myzp_browse_resume_year, myzp_browse_resume_height,
			myzp_browse_resume_account, myzp_browse_resume_phone,
			myzp_browse_resume_email, myzp_browse_resume_address,
			myzp_browse_resume_sex, myzp_browse_resume_person_title,
			myzp_browse_resume_name_title;

	private LinearLayout myzp_browse_resume_icon_name_layout;

	private TextView myzp_browse_resume_edu;
	private TextView myzp_browse_resume_exp;

	private Button myzp_browse_resume_bt;

	private ImageView usercenter_icon_header;

	private static final int CROP_FROM_CAMERA = 2;

	private Bitmap bitmap;

	private String mResumID = "";

	private int mColorID = 0; // 0:默认 1:红色 2:蓝色 3:绿色 4:黄色
	private final int SING_CHOICE_DIALOG = 1;
	
	private ResumeInfo resume = new ResumeInfo();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myzhaopin_browse_resume);
		
		mApp = getMyApplication();

		if (!(ActivityTaskManager.getInstance()
				.containsName("BrowseResumeActivity"))) {
			ActivityTaskManager.getInstance().putActivity(
					"BrowseResumeActivity", this);
		}

		// 返回按钮
		backbutton = (Button) findViewById(R.id.back);
		backbutton.setText("  返回");
		backbutton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity(
						"BrowseResumeActivity");
			}
		});
		// 标题
		headerTitle = (TextView) findViewById(R.id.headTitle);
		headerTitle.setText("简历详情 ");

		top_right_btn = (Button) findViewById(R.id.top_right_button);
		top_right_btn.setText("修改简历");
		top_right_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(BrowseResumeActivity.this,
						EditResumeActivity.class);
				Bundle mBundle = new Bundle(); 
				mBundle.putSerializable("resume", resume);
				intent.putExtras(mBundle);
				startActivity(intent);
				ActivityTaskManager.getInstance().removeActivity(
						"BrowseResumeActivity");
			}
		});

		init();

	}

	private void init() {

		mResumID = getIntent().getStringExtra("resumeid");
		
		usercenter_icon_header = (ImageView) findViewById(R.id.myzp_create_resume_usercenter_icon_header);

		myzp_browse_resume_name = (TextView) findViewById(R.id.myzp_browse_resume_name);
		myzp_browse_resume_user_name = (TextView) findViewById(R.id.myzp_browse_resume_user_name);
		myzp_browse_resume_year = (TextView) findViewById(R.id.myzp_browse_resume_year);
		myzp_browse_resume_height = (TextView) findViewById(R.id.myzp_browse_resume_height);
		myzp_browse_resume_account = (TextView) findViewById(R.id.myzp_browse_resume_account);
		myzp_browse_resume_phone = (TextView) findViewById(R.id.myzp_browse_resume_phone);
		myzp_browse_resume_email = (TextView) findViewById(R.id.myzp_browse_resume_email);
		myzp_browse_resume_address = (TextView) findViewById(R.id.myzp_browse_resume_address);
		myzp_browse_resume_sex = (TextView) findViewById(R.id.myzp_browse_resume_sex);

		myzp_browse_resume_person_title = (TextView) findViewById(R.id.myzp_browse_resume_person_title);
		myzp_browse_resume_name_title = (TextView) findViewById(R.id.myzp_browse_resume_name_title);
		myzp_browse_resume_icon_name_layout = (LinearLayout) findViewById(R.id.myzp_browse_resume_icon_name_layout);

		intiColor(mColorID);

		myzp_browse_resume_edu = (TextView) findViewById(R.id.myzp_browse_resume_edu);
		myzp_browse_resume_exp = (TextView) findViewById(R.id.myzp_browse_resume_exp);

		myzp_browse_resume_bt = (Button) findViewById(R.id.myzp_browse_resume_bt);
		myzp_browse_resume_bt.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(SING_CHOICE_DIALOG);
			}
		});

		new getResumeInfo().execute(URLHelper.mPrefix);

	}

	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case SING_CHOICE_DIALOG:
			Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("请选择风格");
			final ChoiceOnClickListener choiceListener = new ChoiceOnClickListener();
			builder.setSingleChoiceItems(R.array.hobby, 0, choiceListener);

			DialogInterface.OnClickListener btnListener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialogInterface, int which) {

					int choiceWhich = choiceListener.getWhich();  
					intiColor(choiceWhich);
				}
			};
			builder.setPositiveButton("确定", btnListener);
			dialog = builder.create();
			break;
		}
		return dialog;
	}

	private class ChoiceOnClickListener implements
			DialogInterface.OnClickListener {

		private int which = 0;

		@Override
		public void onClick(DialogInterface dialogInterface, int which) {
			this.which = which;
		}

		public int getWhich() {
			return which;
		}
	}

	private void intiColor(int id){
		switch(id){
		case 0:
			myzp_browse_resume_name_title.setTextColor(getResources().getColor(R.color.black));
			myzp_browse_resume_person_title.setTextColor(getResources().getColor(R.color.black));
			
			myzp_browse_resume_person_title.setBackgroundResource(R.drawable.myzp_browse_resume_person_white);
			myzp_browse_resume_icon_name_layout.setBackgroundResource(R.drawable.myzp_browse_resume_icon_name_layout_white);
			break;
		case 1:
			myzp_browse_resume_name_title.setTextColor(getResources().getColor(R.color.resum_browse_red));
			myzp_browse_resume_person_title.setTextColor(getResources().getColor(R.color.resum_browse_red));
			
			myzp_browse_resume_person_title.setBackgroundResource(R.drawable.myzp_browse_resume_person_red);
			myzp_browse_resume_icon_name_layout.setBackgroundResource(R.drawable.myzp_browse_resume_icon_name_layout_red);
			break;
		case 2:
			myzp_browse_resume_name_title.setTextColor(getResources().getColor(R.color.resum_browse_blue));
			myzp_browse_resume_person_title.setTextColor(getResources().getColor(R.color.resum_browse_blue));
			
			myzp_browse_resume_person_title.setBackgroundResource(R.drawable.myzp_browse_resume_person_blue);
			myzp_browse_resume_icon_name_layout.setBackgroundResource(R.drawable.myzp_browse_resume_icon_name_layout_blue);
			break;
		case 3:
			myzp_browse_resume_name_title.setTextColor(getResources().getColor(R.color.resum_browse_green));
			myzp_browse_resume_person_title.setTextColor(getResources().getColor(R.color.resum_browse_green));
			
			myzp_browse_resume_person_title.setBackgroundResource(R.drawable.myzp_browse_resume_person_green);
			myzp_browse_resume_icon_name_layout.setBackgroundResource(R.drawable.myzp_browse_resume_icon_name_layout_green);
			break;
		case 4:
			myzp_browse_resume_name_title.setTextColor(getResources().getColor(R.color.resum_browse_yellow));
			myzp_browse_resume_person_title.setTextColor(getResources().getColor(R.color.resum_browse_yellow));
			
			myzp_browse_resume_person_title.setBackgroundResource(R.drawable.myzp_browse_resume_person_yellow);
			myzp_browse_resume_icon_name_layout.setBackgroundResource(R.drawable.myzp_browse_resume_icon_name_layout_yellow);
			break;
		}
	}

	class getResumeInfo extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "resumeDetail");
			map.put("id", mResumID);
			Log.e("TMG", "this resume id = " + mResumID);
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(BrowseResumeActivity.this, "失败",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(BrowseResumeActivity.this, "网络超时",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else {
				parseJson(result);
			}

		}
	}

	private void parseJson(String str) {

		Log.e("TMG", "resume info = " + str);
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				JSONObject json = new JSONObject(str);
				
				resume = new ResumeInfo();
				resume.setResume_id(mResumID);

				myzp_browse_resume_name.setText(json.getString("title"));
				resume.setResume_name(json.getString("title"));
				
				myzp_browse_resume_user_name
						.setText(json.getString("fullname"));
				resume.setResume_user_name(json.getString("fullname"));
				
				if ("1".equals(json.getString("sex"))) {
					myzp_browse_resume_sex.setText("男");
				} else {
					myzp_browse_resume_sex.setText("女");
				}
				resume.setResume_sex(json.getString("sex"));
				
				myzp_browse_resume_year.setText(json.getString("birthdate") + "年");
				resume.setResume_year(json.getString("birthdate"));
				
				myzp_browse_resume_height.setText(json.getString("height") + "cm");
				resume.setResume_height(json.getString("height"));
				
				myzp_browse_resume_account.setText(json
						.getString("householdaddress"));
				resume.setResume_account(json.getString("householdaddress"));
				
				myzp_browse_resume_phone.setText(json.getString("telephone"));
				resume.setResume_phone(json.getString("telephone"));
				
				myzp_browse_resume_email.setText(json.getString("email"));
				resume.setResume_email(json.getString("email"));
				
				myzp_browse_resume_address.setText(json.getString("address"));
				resume.setResume_address(json.getString("address"));
				
				myzp_browse_resume_exp.setText(json.getString("experience_cn"));
				resume.setResume_exp(json.getString("experience_cn"));
				
				myzp_browse_resume_edu.setText(json.getString("education_cn"));
				resume.setResume_edu(json.getString("education_cn"));
				
				if ("".equals(json.getString("photo_img"))) {

				}else{
					Log.e("TAG", "mApp.user.getuID() = " + mApp.user.getuID());
					Log.e("TAG", "img url = " + URLHelper.mPrefixImg + json.getString("photo_img"));
					mApp.imageLoader.DisplayImage(URLHelper.mPrefixImg + json.getString("photo_img"),
							usercenter_icon_header, R.drawable.icon_header);
				}

			}
		} catch (JSONException e) {
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"BrowseResumeActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
